package com.example.demo.service.order.impl;

import com.alibaba.fastjson.JSON;
import com.example.demo.common.BaseErrorConfigEnum;
import com.example.demo.common.BasePageResponse;
import com.example.demo.common.ResultObject;
import com.example.demo.common.TerminologyConfigEnum;
import com.example.demo.dao.IVpsDao;
import com.example.demo.jooq.tables.daos.VpsDao;
import com.example.demo.jooq.tables.pojos.Vps;
import com.example.demo.model.po.VpsPage;
import com.example.demo.model.request.VpsPageRequest;
import com.example.demo.model.request.VpsRequest;
import com.example.demo.model.response.VpsResponse;
import com.example.demo.service.BaseBusinessServiceImpl;
import com.example.demo.service.order.IVpsService;
import com.example.demo.util.AssertUtils;
import com.example.demo.util.ClazzUtils;
import com.example.demo.util.DateUtils;
import org.modelmapper.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class IVpsServiceImpl extends BaseBusinessServiceImpl implements IVpsService {
    @Autowired
    private IVpsDao iVpsDao;

    @Autowired
    private VpsDao vpsDao;
    @Override
    public ResultObject<BasePageResponse<VpsResponse>> queryPageVpsList(VpsPageRequest vpsPageRequest) {
        ResultObject<BasePageResponse<VpsResponse>> responseResultObject = new ResultObject<>();
        List<VpsPage> vpsPages =  iVpsDao.queryPageVpsList(vpsPageRequest);
        if(AssertUtils.isNotEmpty(vpsPages)){
            Integer integer =vpsPages.get(0).getTotalLine();
            List<VpsResponse> vpsResponses = (List<VpsResponse>)this.converterList(vpsPages,new TypeToken<List<VpsResponse>>() {}.getType() );
            //术语转换
            vpsResponses.forEach(visaResponse -> {
                if(AssertUtils.isNotNull(visaResponse.getVpsStatus())){
                    Arrays.stream(TerminologyConfigEnum.VPS_STATUS.values()).filter(value->value.name().equals(visaResponse.getVpsStatus())).forEach(value->visaResponse.setVpsStatus(value.desc()));
                }
            });
            BasePageResponse<VpsResponse> responseBasePageResponse =BasePageResponse.getData(vpsPageRequest.getCurrentPage(),vpsPageRequest.getPageSize(),integer,vpsResponses);
            responseResultObject.setData(responseBasePageResponse);
        }
        return responseResultObject;
    }

    @Override
    public ResultObject saveVps(VpsRequest vpsRequest) {
        this.getLogger().info("保存vps,请求数据为:{}", JSON.toJSONString(vpsRequest));
        ResultObject resultObject = new ResultObject();
        //数据校验
        vaildVpsParameter(vpsRequest);
        Vps vps=null;
        if(AssertUtils.isNotNull(vpsRequest.getVpsId())){
            vps=vpsDao.fetchOneByVpsId(vpsRequest.getVpsId());
            AssertUtils.isNotNull(this.getLogger(),vps,BaseErrorConfigEnum.VPS_NOT_NULL);
        }else {
            vps=new Vps();
        }
        ClazzUtils.copyProperties(vpsRequest,vps);

        //日期处理
        if(AssertUtils.isNotEmpty(vpsRequest.getStartDate())){
            vps.setStartDate(DateUtils.stringToTime(vpsRequest.getStartDate()));
        }
        if(AssertUtils.isNotEmpty(vpsRequest.getEndDate())){
            vps.setEndDate(DateUtils.stringToTime(vpsRequest.getEndDate()));
        }

        //持久化
        if(AssertUtils.isNotEmpty(vpsRequest.getVpsId())){
            vps.setUpdatedDate(System.currentTimeMillis());
            vpsDao.update(vps);
        }else {
            vps.setVpsStatus(AssertUtils.isNotNull(vpsRequest.getVpsStatus())?vpsRequest.getVpsStatus():TerminologyConfigEnum.EMAIL_STATUS.NEW.name());
            vps.setVpsId(DateUtils.getJobNumberByTime2("VPS",null,DateUtils.FORMATE53,false));
            vps.setCreatedDate(System.currentTimeMillis());
            vps.setUpdatedDate(System.currentTimeMillis());
            vpsDao.insert(vps);
        }
        return resultObject;
    }

    private void vaildVpsParameter(VpsRequest vpsRequest) {
        AssertUtils.isNotNull(this.getLogger(),vpsRequest.getVpsIp(), BaseErrorConfigEnum.VPS_IP_NOT_NULL);
        AssertUtils.isNotNull(this.getLogger(),vpsRequest.getChannelUser(),BaseErrorConfigEnum.VPS_USER_NOT_NULL);
        AssertUtils.isNotNull(this.getLogger(),vpsRequest.getChannel(),BaseErrorConfigEnum.VPS_CHANNEL_NOT_NULL);
        AssertUtils.isNotNull(this.getLogger(),vpsRequest.getChannelUrl(), BaseErrorConfigEnum.VPS_CHANNEL_URL_NOT_NULL);
    }

    @Override
    public ResultObject<VpsResponse> queryVpsDetail(String vpsId) {
        ResultObject<VpsResponse> resultObject =new ResultObject<>();
        Vps vps = vpsDao.fetchOneByVpsId(vpsId);
        if(AssertUtils.isNotNull(vps)){
            VpsResponse vpsResponse =(VpsResponse)this.converterObject(vps,VpsResponse.class);
            resultObject.setData(vpsResponse);
        }
        return resultObject;
    }
}
